package com.ruoyi.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.cg.pojo.Community;
import com.ruoyi.common.core.cg.pojo.Leader;
import com.ruoyi.common.core.cg.result.Result;
import com.ruoyi.manage.mapper.*;
import com.ruoyi.manage.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author 马景昌
 * @create 2025/03/12
 * @description
 */
@Service
public class CommunityServiceImpl implements CommunityService {
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private LeaderMapper leaderMapper;
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public Result show() {
        List<Community> show = communityMapper.show();
        return new Result(true,"查询成功",show);
    }
    @Override
    public Result pageShow(Community community) {
//        if(pageNum==0 && pageSize==0){
//            pageNum=1;
//            pageSize=5;
//        }
        PageHelper.startPage(community.getPageNum(),community.getPageSize());
        List<Community> communities = communityMapper.pageShow(community);
        for (Community community1 : communities) {
            //根据leaderId来查询团长
            Leader leader = leaderMapper.selectByid(community1.getLeaderId());
            if(leader!=null){
                community1.setLeaderName(leader.getName());
            }else {
                community1.setLeaderName("当前没有负责人");
            }
            //查询拼团数量
            Integer groupCount = groupMapper.selectGroupCount(community1.getId());
            community1.setGroupCount(groupCount);
            //查询商铺数量
            Integer storeCount = storeMapper.selectStoreCount(community1.getId());
            community1.setStoreCount(storeCount);
            //查询拼团点的用户
            Integer userCount = userMapper.selectUserCount(community1.getId());
            System.err.println(userCount+"group user");
            community1.setUserCount(userCount);
            //已提货订单0
            Integer order0 = communityMapper.selectOrder0(community1.getId());
            community1.setOrderCount0(order0);
            //未提货订单1
            Integer order1 = communityMapper.selectOrder1(community1.getId());
            community1.setOrderCount1(order1);
            //订单总金额
            BigDecimal price = communityMapper.price(community1.getId());
            if(price==null){
                community1.setMoney(BigDecimal.valueOf(0));
            }else {
                community1.setMoney(price);
            }
//            community1.setCreateTime();
        }
        PageInfo<Community> pageInfo = new PageInfo<>(communities);
        System.err.println(pageInfo.getTotal());
        return new Result(true,"查询成功",pageInfo);
    }

    @Override
    public Result info(Integer id) {
        Community info = communityMapper.info(id);
        return new Result(true,"查询成功",info);
    }

    @Override
    public Result put(Community community) {
        communityMapper.put(community);
        return new Result(true,"修改成功");
    }

    @Override
    public List<Community> All() {
        List<Community> all = communityMapper.All();
        return all;
    }


}
